/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  FromActivityTagXMLToTree.java                                      
 *  File Contents Outline :  建立过滤规则树                  
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.activity.abstraction;

import cn.edu.thu.log.tree.ManyTreeNode;
import cn.edu.thu.log.tree.XMLNode;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import org.xml.sax.InputSource;
import storage.dbhelper.ActivityAbstractHelper;
import storage.dbobject.DbActivityAbstractRule;

/**
 * get config message from activity tag file
 * @author wan
 */
public class FromActivityTagXMLToTree{
    private Element childelement;
    private String minValue;
    private String maxValue;

    public Element readActivityXML(String filename) {
        // parse the XML as a W3C Document
        //使用SAXBuilder对象
 //       System.out.println("readXML activity tag config filename is:" + filename);
        String activityTagXMLString="";
        ActivityAbstractHelper noiseHelper = new ActivityAbstractHelper();
        List<DbActivityAbstractRule> activityAbstractRules = noiseHelper.getActivityAbstractRules(DbActivityAbstractRule.cl_name, filename);
        DbActivityAbstractRule activityAbstractRule;

        if (activityAbstractRules.size()>0) {
            activityAbstractRule = activityAbstractRules.get(0);
//            activityTagXMLString = activityAbstractRule.getAbstractRuleList();
        }
        
        SAXBuilder sax = new SAXBuilder();
        StringReader sr = new StringReader(activityTagXMLString);
        InputSource source = new InputSource(sr);

        //到数据库读取filename的xml文件      
        Document doc = null;
        Element root = null;
        try {
//            doc = sax.build("E:\\treerule4.xml");
            doc = sax.build(source);
            Element xmlroot = doc.getRootElement();// 得到根节点

//            Element max = xmlroot.getChild("maxValue");
//            Element min = xmlroot.getChild("minValue");
//            maxValue = max.getAttributeValue("value");
//            minValue = min.getAttributeValue("value");
//            System.out.println("minValue is:" + getMinValue() + " maxValue is:" + getMaxValue());

            Element treeroot = xmlroot.getChild("div");
            if (treeroot.getAttributeValue("class").equals("dtree")) {
    //            System.out.println("get tree root file!");
            }

            root = (Element) XPath.selectSingleNode(treeroot, "./div[@id]/div[@class=\"dTreeNode\"]");

        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return root;
    }

    public ManyTreeNode createTree(Element node) throws JDOMException {
        int flag = 0;
        Element p = (Element) XPath.selectSingleNode(node, "./a[@class=\"node\"]");
        String nodename = p.getText();
//        System.out.println("treenode name!");
        String type = p.getAttributeValue("type");
        String id = p.getAttributeValue("id").substring(2);
   //     System.out.println("create Tree!");
   //     System.out.println("nodename:" + nodename + "  type:" + type + "  id:" + id);
        XMLNode xmlnode = new XMLNode(id, nodename, type);

        ArrayList<ManyTreeNode> childList = new ArrayList<ManyTreeNode>();
        ManyTreeNode rootNode = new ManyTreeNode(xmlnode, childList);

        //创建子节点列表
        List neighbourWithChild = node.getParentElement().getChildren();
        String currentid = p.getAttributeValue("id").substring(2);
        Element e = null;
        for (int i = 0; i < neighbourWithChild.size(); i++) {
            e = (Element) neighbourWithChild.get(i);
            //找到它的孩子节点集e
            if (e.getAttributeValue("id") != null && e.getAttributeValue("id").substring(2).equals(currentid)) {
                childelement = e;

                List childNodeList = childelement.getChildren();
                if (childNodeList.isEmpty()) {
 //                   System.out.println("child size is:" + childNodeList.size());
                    return rootNode;
                } else {
                    Element c = null;
                    for (int j = 0; j < childNodeList.size(); j++) {
                        c = (Element) childNodeList.get(j);
                        if (c.getAttributeValue("class").equals("dTreeNode")) {
                            childList.add(createTree(c));
                        }
                    }
                    rootNode.setChildList(childList);
                }
            }
        }
        return rootNode;
    }
}
